package com.maotai.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.http.client.config.RequestConfig;

import cn.proxy.util.IPFactory;

import com.alibaba.fastjson.JSONObject;
import com.maotai.bean.MaotaiString;
import com.maotai.bean.MaotaiWebCookie;
import com.maotai.dao.MaotaiWebDao;
import com.maotai.util.HttpClientUtil;

public class MaoTaiRegisterThreadSG2 extends Thread {
	private Map<String, String> sessionMap;
	private String telphone;

	public MaoTaiRegisterThreadSG2(Map<String, String> sessionMap,
			String telphone) {
		this.sessionMap = sessionMap;
		this.telphone = telphone;
	}

	public Map<String, String> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(Map<String, String> sessionMap) {
		this.sessionMap = sessionMap;
	}

	public String getTelphone() {
		return telphone;
	}

	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}

	@Override
	public void run() {
		try {
			MaotaiWebCookie mwc = new MaotaiWebCookie();
			HttpClientUtil hcu = HttpClientUtil.getInstance();
			MaotaiWebDao mwd = new MaotaiWebDao();
			
			String csessionid = sessionMap.get("alicdnsession") + "";
			String sig = sessionMap.get("alicdnsig") + "";
			String token = sessionMap.get("alicdntoken") + "";
			/**
			 * action:SendSms phone:13372007847 session:0152
			 * JIZgtMjy7iQLwB8JakWcbcUB1vSN2w_67jUN4TsaDGfN8zUzh9pyAO
			 * -KKhOFMD5uC96FjMrxqr
			 * -__qr9xqxeWPVt6Z56jZipl0ywd9_9GI8gRxTVvCBgRcQWPjKRt5RZ1y8H76
			 * -sUS-UmCT4sv8Y3qimr-_g_w-m3mW4nE4pY sig:05
			 * a1C7nT4bR5hcbZlAujcdyXquG0EzLazJTxThdXHob4Cwz_CHC7mVXTNOl3fRxLfX7B9mSNQmayqFCDqgMuVErjffNWenW0I5Rzqx
			 * -9JkU1liGp4-RSlo2wwzH2kqN58cva1LfZVsHgRxuC-
			 * oD6LZtgWBOrcBi6hOKuPcOFFIdimACQn1TtO
			 * -c3PzG2Gb0-slGtpeI_L-3pfKMLXM15-dLCbaX1r-fT8O1JPlaX-
			 * a7WCWgmgi7AGXsJEgF4Tuazrm41dDxumalIfvNEZ0UGpepjTNdh6KIdm5Q4m3A9WzAwczbL6N4Rdaw3G9_b4MSe0ZUcVH3RasW5OAwlNar2UwpXToAOWhd1TifWpv9F9uFW2oS_uGUQ3B_xqvr1iwSgfii4F
			 * -vjUaS09FHOiNXQWeYm1ESrc7fDk_36pG5ps4gXg
			 * token:FFFF00000000016A8646:1512908534458:0.9088320197698758
			 * scene:register register:register
			 */
//			RequestConfig config = IPFactory.getQWSingleRequestConfig();
			Map<String, String> registerMap = new HashMap<String, String>();
			registerMap.put("action", "SendSms");
			registerMap.put("phone", telphone);
			registerMap.put("session", csessionid);
			registerMap.put("sig", sig);
			registerMap.put("token", token);
			registerMap.put("scene", "register");
			registerMap.put("register", "register");
			mwc = hcu
					.sendHttpPostSendSmsProxy(MaotaiString.SENDSCM_URL, mwc,
							registerMap,
							"https://www.emaotai.cn/Register.aspx", IPFactory.getQWSingleRequestConfig());
			// mwc = hcu.sendHttpPostSendSms(MaotaiString.SENDSCM_URL, mwc,
			// registerMap, "https://www.emaotai.cn/Register.aspx");

			System.out.println(mwc.getContent());
			while(mwc.getContent().contains("!DOCTYPE")){
				mwc = hcu.sendHttpPostSendSmsProxy(MaotaiString.SENDSCM_URL, mwc,
								registerMap,
								"https://www.emaotai.cn/Register.aspx", IPFactory.getQWSingleRequestConfig());
			}
			if (mwc.getContent() != null && !"".equals(mwc.getContent())) {
				// {"retcode":1,"retmsg":"您的手机已注册！","data":""}
				JSONObject jObject = JSONObject.parseObject(mwc.getContent());
				if (jObject.getString("retmsg").contains("您的手机已注册")) {
					System.out.println("手机号已注册");
					registerMap.clear();
					registerMap.put("code", telphone);
					registerMap.put("pwd", telphone);
					// 从数据库中查询，有记录则继续循环，没有就插入
					mwd.insertRegister(registerMap);
				} else if ("success".equals(jObject.getString("retmsg"))) {
					System.out.println("手机号未注册，等待获取验证码....");
					// 改为从嘻唰唰数据库中获取验证码
					registerMap.clear();
					registerMap.put("telphone", telphone);
					String telCode = "";
					for (int i = 0; i < 20; i++) {
						Map<String, String> map = mwd.getTelCode(registerMap);
						if (map != null && !map.isEmpty()
								&& map.get("msgtitle") != null
								&& !"".equals(map.get("msgtitle"))) {
							telCode = map.get("msgtitle");
							break;
						} else {
							Thread.sleep(3000);
						}

					}
					System.out.println(telphone + "--获取到的验证码信息为：" + telCode);
					if (telCode == null || "".equals(telCode)) {
						return;
					}
					if ("超时".equals(telCode)) {
						try {
							throw new Exception("未获取到验证码");
						} catch (Exception e) {
						}
					}
					String regEx = "[^0-9]";
					Pattern p = Pattern.compile(regEx);
					telCode = p.matcher(telCode).replaceAll("").trim()
							.substring(0, 6);
					System.out.println("截取后的验证码信息为:" + telCode);

					/**
					 * __VIEWSTATE:/
					 * wEPDwUJNzA2NDkyNTI2D2QWAmYPZBYCZg9kFgICAg9kFgQCAQ8WAh4EVGV4dAULbWVudWN1cnJlbnRkAhMPEGRkFgBkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBRFyZWdpc3RlciRjaGtBZ3JlZSQHaKigSg
					 * +ZProwhbZCl4j/k2nA __VIEWSTATEGENERATOR:799CC77D
					 * register$txtUserName:17086204036
					 * register$txtEmail:17086204036@126.com
					 * register$txtCellPhone:17086204036
					 * register$txtMsgCode:997679
					 * register$txtPassword:17086204036
					 * register$txtPassword2:17086204036 register$chkAgree:on
					 * register$btnRegister:立即注册 csessionid:0152
					 * JIZgtMjy7iQLwB8JakWeYzaHW1xKEjrd2Z6SEApoMHzAZyuza5qbz1XyHAZUV8ChvAH6vCQehrWGNd1Favwpk0q2Mi4gwNEcesiLdLdOqI8gRxTVvCBgRcQWPjKRt5RZ1y8H76
					 * -sUS-UmCT4sv8Y3qimr-_g_w-m3mW4nE4pY sig:05
					 * a1C7nT4bR5hcbZlAujcdyXquG0EzLazJTxThdXHob4Cwz_CHC7mVXTNOl3fRxLfX_Ot9tejZ8QfXXGpRCFDBpa
					 * -56T6Iwdnp0CoxprWZlCFiGp4-
					 * RSlo2wwzH2kqN58cYmNBanoxKSM0KI_dhJJEF9qvwHEgU9J2fSgyEW2WFiXmgfur3KX8VYwReF722yV4_PNxdegBXl9WB5AVP1ZbFfhwJ8C6tX_yAt2XB30kTLjUh_SUafEgPXmdSssCqokg41dDxumalIfvNEZ0UGpepjTNdh6KIdm5Q4m3A9WzAwczbL6N4Rdaw3G9_b4MSe0ZtcATJ_jdTs4VdmBVBFms_JQ
					 * -
					 * JmRsNUrBrtKcysunPX6A6po73EnM5wGgK6Jy8dAahqRhmLPFOv9XV5mNRDQ6FGEYrZh6N_t_KpwVZST0JmA
					 * token
					 * :FFFF00000000016A8646:1512908604982:0.8650003992690907
					 * scene:register
					 */
					registerMap.clear();
					registerMap
							.put("__VIEWSTATE",
									"/wEPDwUJNzA2NDkyNTI2D2QWAmYPZBYCZg9kFgICAg9kFgQCAQ8WAh4EVGV4dAULbWVudWN1cnJlbnRkAhMPEGRkFgBkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBRFyZWdpc3RlciRjaGtBZ3JlZSQHaKigSg+ZProwhbZCl4j/k2nA");
					registerMap.put("__VIEWSTATEGENERATOR", "799CC77D");
					registerMap.put("register$txtUserName", telphone);
					registerMap.put("register$txtEmail", telphone + "@126.com");
					registerMap.put("register$txtCellPhone", telphone);
					registerMap.put("register$txtMsgCode", telCode);
					registerMap.put("register$txtPassword", telphone);
					registerMap.put("register$txtPassword2", telphone);
					registerMap.put("register$chkAgree", "on");
					registerMap.put("register$btnRegister", "立即注册");
					registerMap.put("csessionid", csessionid);
					registerMap.put("sig", sig);
					registerMap.put("token", token);
					registerMap.put("scene", "register");
					mwc = hcu.sendHttpPostRegisterProxy(
							MaotaiString.REGISTER_URL, mwc, registerMap,
							"https://www.emaotai.cn/Register.aspx", IPFactory.getQWSingleRequestConfig());
					// mwc = hcu.sendHttpPostRegister(MaotaiString.REGISTER_URL,
					// mwc, registerMap,
					// "https://www.emaotai.cn/Register.aspx");
//					if (mwc.getContent() != null
//							&& !"".equals(mwc.getContent())) {
//						System.out.println(telphone + "--注册成功");
//						registerMap.clear();
//						registerMap.put("code", telphone);
//						registerMap.put("pwd", telphone);
//						mwd.insertRegister(registerMap);
//					}
					//不等返回了，直接等5秒，插入到数据库
					Thread.sleep(5000);
					System.out.println(telphone + "--注册成功");
					registerMap.clear();
					registerMap.put("code", telphone);
					registerMap.put("pwd", telphone);
					mwd.insertRegister(registerMap);
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
